//
//  BrandViewController.m
//  DuBai
//
//  Created by darktm on 14-2-26.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "BrandViewController.h"

#import "BrandViewCell.h"

@interface BrandViewController ()
{
    NSMutableArray *friendsListArray;
    NSMutableArray *sectionsArray;
    UILocalizedIndexedCollation *collation;
}
@end

@implementation BrandViewController

#define TAG_CELL 10000

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.btnComplete.layer.cornerRadius = 13.0f;
    
    NSArray *names = [NSMutableArray arrayWithArray:@[@"白飞",@"andy",@"张冲",@"林峰",@"kylin",@"etwei",@"王磊",@"emily",@"陈标",@"billy",@"韦丽"]];
    
    friendsListArray = [NSMutableArray arrayWithCapacity:10];
    for (int i = 0; i<[names count]; i++) {
        BrandObject *obj = [[BrandObject alloc] init];//[[BrandObject alloc] initWithFriends:names[i]];
        
        obj.localeName = names[i];
        obj.bSelected = NO;
        [friendsListArray addObject:obj];
    }
    
    [self configureSections];
    
}

#pragma mark -
#pragma mark Table view data source and delegate methods
//设置Section的数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [sectionsArray count];
}

//设置每个Section下面的cell数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // The number of time zones in the section is the count of the array associated with the section in the sections array.
    NSArray *friendsInSection = [sectionsArray objectAtIndex:section];
    
    return (int)ceilf([friendsInSection count] / 2.0);
}

-(void)on_btn_brand:(UIButton *)btn{

    DLog(@"%@",btn.superview);

    UIView *bg = btn.superview;
    UIView *image = [bg viewWithTag:btn.tag + 1];

    image.hidden = !image.hidden;
    
    
    int section = bg.tag / TAG_CELL - 1;
    int row = bg.tag % TAG_CELL;
    
    NSMutableArray *friendsInSection = [NSMutableArray arrayWithArray:[sectionsArray objectAtIndex:section]];
    
    if(btn.tag == 1)
        row = row * 2;
    else
        row = row * 2 + 1;
    
    BrandObject *obj = [friendsInSection objectAtIndex:row];
    obj.bSelected = !image.hidden;
    
//    [friendsInSection replaceObjectAtIndex:row withObject:obj];
//    [sectionsArray replaceObjectAtIndex:section withObject:friendsInSection];
}

//设置每行的cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BrandViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BrandViewCell" forIndexPath:indexPath];
    
    cell.contentView.tag = TAG_CELL * (indexPath.section + 1) + indexPath.row ;

    NSArray *friendsInSection = [sectionsArray objectAtIndex:indexPath.section];
    
    
    if(friendsInSection.count >= indexPath.row * 2 + 2){//有2个供显示
        BrandObject *friend1 = [friendsInSection objectAtIndex:2 * (indexPath.row)];
        BrandObject *friend2 = [friendsInSection objectAtIndex:2 * (indexPath.row) + 1];
        
        cell.image1.hidden = NO;
        cell.btn1.hidden = NO;
        
        if(friend1.bSelected)
            cell.image1.hidden = NO;
        else
            cell.image1.hidden = YES;
        
        cell.image2.hidden = NO;
        cell.btn2.hidden = NO;
        
        if(friend2.bSelected)
            cell.image2.hidden = NO;
        else
            cell.image2.hidden = YES;
        
        [cell.btn1 addTarget:self action:@selector(on_btn_brand:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn2 addTarget:self action:@selector(on_btn_brand:) forControlEvents:UIControlEventTouchUpInside];
        
    }else{
        
        BrandObject *friend1 = [friendsInSection objectAtIndex:2 * (indexPath.row)];
        cell.image1.hidden = NO;
        cell.btn1.hidden = NO;
        
        if(friend1.bSelected)
            cell.image1.hidden = NO;
        else
            cell.image1.hidden = YES;
        
        [cell.btn1 addTarget:self action:@selector(on_btn_brand:) forControlEvents:UIControlEventTouchUpInside];
        cell.image2.hidden = YES;
        cell.btn2.hidden = YES;
    }
    
    
    return cell;
}

//设置section的Header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    //NSLog(@"%d",[[self.sectionsArray objectAtIndex:section]count]);
    if ([[sectionsArray objectAtIndex:section]count] == 0)
    {
        
        return nil;
    }
    
    return [[collation sectionTitles] objectAtIndex:section];
}

//设置索引标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [collation sectionIndexTitles];
}

//关联搜索
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [collation sectionForSectionIndexTitleAtIndex:index];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DLog(@"向上取整： %d / 9 = ceilf %d",indexPath.section,(int)ceilf(indexPath.section / 9.0));
    DLog(@"向下取整： %d / 9 = ceilf %d",indexPath.section,(int)floorf(indexPath.section / 9.0));
    DLog(@"四舍五入： %d / 9 = ceilf %d",indexPath.section,(int)nearbyintf(indexPath.section / 9.0));
}

#pragma mark -
#pragma mark Set the data array and configure the section data


- (void)configureSections {
    
    //获得当前UILocalizedIndexedCollation对象并且引用赋给collation
    collation = [UILocalizedIndexedCollation currentCollation];
    //获得索引数和section标题数
    NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];
    
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    
    
    //    - (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;
    
    
    
    //    NSArray *a = [collation sortedArrayFromArray:friendsListArray collationStringSelector:@selector(localeName)];
    //
    //    sectionsArray = [NSMutableArray arrayWithArray:a];
    //
    //    return;
    //设置sections数组：元素包含timezone
    for (index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
    }
    
    // Segregate the time zones into the appropriate arrays.
    for (BrandObject *friend in friendsListArray) {
        
        //根据timezone的localename，获得对应的时区的section number
        NSInteger sectionNumber = [collation sectionForObject:friend collationStringSelector:@selector(localeName)];
        
        //获得section的数组
        NSMutableArray *sectionFriends = [newSectionsArray objectAtIndex:sectionNumber];
        
        //添加时区内容到section中 sectionTimeZones 存放的是所有的字符串
        [sectionFriends addObject:friend];
    }
    
    //排序
    for (index = 0; index < sectionTitlesCount; index++)
        
    {
        NSMutableArray *friendsListArrayForSection = [newSectionsArray objectAtIndex:index];
        
        //获得排序结果
        NSArray *sortedFriendsArrayForSection = [collation sortedArrayFromArray:friendsListArrayForSection collationStringSelector:@selector(localeName)];
        
        //替换原来数组
        [newSectionsArray replaceObjectAtIndex:index withObject:sortedFriendsArrayForSection];
    }
    
    sectionsArray = newSectionsArray;
}

- (IBAction)on_btn_complete:(UIButton *)sender {
}
@end


@implementation BrandObject


@end
